package com.zheng.umbrella.web.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.umbrella.common.constant.UmbrellaResult;
import com.zheng.umbrella.common.constant.UmbrellaResultConstant;
import com.zheng.umbrella.dao.model.UmbrellaRuleSpecialconfig;
import com.zheng.umbrella.dao.model.UmbrellaRuleSpecialconfigDetail;
import com.zheng.umbrella.dao.model.UmbrellaRuleSpecialconfigDetailExample;
import com.zheng.umbrella.dao.model.UmbrellaRuleSpecialconfigExample;
import com.zheng.umbrella.rpc.api.UmbrellaRuleSpecialconfigDetailService;
import com.zheng.umbrella.rpc.api.UmbrellaRuleSpecialconfigService;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个性规则配置controller
 * Created by shuzheng on 2017/2/6.
 */
@Controller
@Api(value = "代理个性规则配置", description = "代理个性规则配置")
@RequestMapping("/manage/specialconfigdetail")
public class UmbrellaRuleSpecialConfigDetailController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UmbrellaRuleSpecialConfigDetailController.class);

    @Autowired
    private UmbrellaRuleSpecialconfigDetailService umbrellaRuleSpecialconfigDetailService;
    @Autowired
    private UmbrellaRuleSpecialconfigService umbrellaRuleSpecialconfigService;
    @Autowired
    private UpmsApiService upmsApiService;


    @ApiOperation(value = "代理修改个性规则配置")
    @RequiresPermissions("umbrella:specialconfigdetail:update")
    @RequestMapping(value = "/update", method = {RequestMethod.GET})
    public String update(ModelMap modelMap) {
        //获取登陆用户代理
        String userName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(userName);
        //加载基础配置
        UmbrellaRuleSpecialconfigExample umbrellaRuleSpecialconfigExample = new UmbrellaRuleSpecialconfigExample();
        //UmbrellaRuleSpecialconfigExample.Criteria configCriteria = umbrellaRuleSpecialconfigExample.createCriteria();
        List<UmbrellaRuleSpecialconfig> specialConfigs = umbrellaRuleSpecialconfigService.selectByExample(umbrellaRuleSpecialconfigExample);
        modelMap.put("specialConfigs", specialConfigs);
        //加载代理配置
        UmbrellaRuleSpecialconfigDetailExample umbrellaRuleSpecialconfigDetailExample = new UmbrellaRuleSpecialconfigDetailExample();
        UmbrellaRuleSpecialconfigDetailExample.Criteria detailCriteria = umbrellaRuleSpecialconfigDetailExample.createCriteria();
        detailCriteria.andAgentIdEqualTo(agentId);
        List<UmbrellaRuleSpecialconfigDetail> agentConfigs = umbrellaRuleSpecialconfigDetailService.selectByExample(umbrellaRuleSpecialconfigDetailExample);
        //未配置，则初始化
        if(CollectionUtils.isEmpty(agentConfigs)){
            for (UmbrellaRuleSpecialconfig spConfig : specialConfigs) {
                UmbrellaRuleSpecialconfigDetail newAgentConfig = new UmbrellaRuleSpecialconfigDetail();
                newAgentConfig.setCode(spConfig.getCode());
                newAgentConfig.setStatus(Constant.CONST_DISABLE);//禁用
                newAgentConfig.setValue("");
                newAgentConfig.setAgentId(agentId);
                umbrellaRuleSpecialconfigDetailService.insert(newAgentConfig);
            }
            agentConfigs = umbrellaRuleSpecialconfigDetailService.selectByExample(umbrellaRuleSpecialconfigDetailExample);
        }
        modelMap.put("agentConfigs", agentConfigs);
        return "/manage/specialconfigdetail/update.jsp";
    }

    @ApiOperation(value = "代理修改个性规则配置")
    @RequiresPermissions("umbrella:specialconfig:update")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public Object update() {
        //获取登陆用户代理
        String userName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(userName);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        //加载基础配置
        UmbrellaRuleSpecialconfigExample umbrellaRuleSpecialconfigExample = new UmbrellaRuleSpecialconfigExample();
        //UmbrellaRuleSpecialconfigExample.Criteria configCriteria = umbrellaRuleSpecialconfigExample.createCriteria();
        List<UmbrellaRuleSpecialconfig> specialConfigs = umbrellaRuleSpecialconfigService.selectByExample(umbrellaRuleSpecialconfigExample);

        //按基础配置更新数据
        int count = 0;
        UmbrellaRuleSpecialconfigDetailExample umbrellaRuleSpecialconfigDetailExample = new UmbrellaRuleSpecialconfigDetailExample();
        for (UmbrellaRuleSpecialconfig spConfig : specialConfigs) {
            umbrellaRuleSpecialconfigDetailExample.clear();
            UmbrellaRuleSpecialconfigDetailExample.Criteria detailCriteria = umbrellaRuleSpecialconfigDetailExample.createCriteria();
            detailCriteria.andAgentIdEqualTo(agentId);
            detailCriteria.andCodeEqualTo(spConfig.getCode());
            String value = request.getParameter(spConfig.getCode());
            String status = request.getParameter(spConfig.getCode()+"_status");
            if(value == null){continue;}
            UmbrellaRuleSpecialconfigDetail newAgentConfig = new UmbrellaRuleSpecialconfigDetail();
            if("1".equals(status)) {
                newAgentConfig.setStatus(Constant.CONST_ENABLE);
            }else{
                newAgentConfig.setStatus(Constant.CONST_DISABLE);
            }
            newAgentConfig.setValue(value);
           int ret  = umbrellaRuleSpecialconfigDetailService.updateByExampleSelective(newAgentConfig,umbrellaRuleSpecialconfigDetailExample);
           count = count + ret;
        }
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }
}
